# 查
# 1.
person = {'name': 'tom', 'age': 18, 'school': 'ustc'}
print(person['school'])  # key 相当于 index
# 当索引的key不存在时会报错
# person['hah']  # KeyError: 'hah'
# 2. get(key, 默认值 = None)方法
""" Return the value for key if key is in the dictionary, else default. """
print(person.get('age'))  # 18
print(person.get('hah'))  # None
print(person.get('hah', '给定的默认值'))

# 增，改
print(person)
person['height'] = 187
person['age'] = 23
print(person)

# 删
# 1.  person.clear()
#  """ D.clear() -> None.  Remove all items from D. """
# 2.  pop(self, key, d=None)
"""
    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
    If key is not found, d is returned if given, otherwise KeyError is raised
"""
print(person.pop('name'))
print(person)
# 3.  popitem()
"""
    D.popitem() -> (k, v), remove and return some (key, value) pair as a
    2-tuple; but raise KeyError if D is empty.
"""
print(person.popitem())
print(person)
